'use strict';

angular.module('memoriesApp')
.factory('photoService', function(dataStoreFactory, global) {

  var settingStore = dataStoreFactory.get('settings', true);
  var qualityMap = global.qualityMap;
  var defaultSettings = global.settings;

  // take photo
  function takePhoto(callback) {
    if (!navigator.camera || !Camera) {
      callback(true, 'Camera is not available');
      return;
    }

    var qualityValue, saveInGallery;
    settingStore.getItem(function(err, item) {
      if (err) {
        item = defaultSettings;
      }
      item = _.defaults(item, defaultSettings);
      qualityValue = qualityMap[item.photoQuality || 'average'];
      saveInGallery = !!item.saveInGallery;
      navigator.camera.getPicture(
        function(imageURI) {
          callback(null, imageURI);
        },
        function(message) {
          callback(true, message);
        },
        {
          quality: qualityValue,
          saveToPhotoAlbum: saveInGallery,
          destinationType: Camera.DestinationType.FILE_URI
        }
      );
    });
  }

  function deletePhoto(uri, callback) {
    // TODO
    // if (!uri) {
    //   callback && callback(true, 'File uri is invalid');
    //   return;
    // }
    // window.resolveLocalFileSystemURI(uri, function(fileEntry) {
    //   fileEntry.remove(function() {
    //     callback && callback();
    //   }, function(err) { callback && callback(true, err); });
    // }, function(error) {
    //   callback && callback(true, error);
    // });
    callback && callback();
  }

  return {
    takePhoto: takePhoto,
    deletePhoto: deletePhoto
  };

});